package com.nike.amoeba.adapter

import android.view.View
import android.widget.Filter
import android.widget.Filterable
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.flyco.roundview.RoundTextView
import com.nike.amoeba.R
import com.nike.amoeba.bean.DataBean
import com.nike.amoeba.bean.db.TaskStockListBean
import com.nike.amoeba.ext.setAdapterAnimation
import com.nike.amoeba.util.SettingUtil
import com.nike.amoeba.weight.MYListView
import org.json.JSONArray
import org.json.JSONObject

/**
 * @Author nike
 * @Date 2023/7/7 17:05
 * @Description
 */
class TaskStockListAdapter (data: ArrayList<TaskStockListBean>) :
    BaseQuickAdapter<TaskStockListBean, BaseViewHolder>(
        R.layout.i_task_asset, data), Filterable {

    init {
        setAdapterAnimation(SettingUtil.getListMode())
    }

    override fun convert(viewHolder: BaseViewHolder, bean: TaskStockListBean) {
        mFilterList[viewHolder.layoutPosition].run {
            viewHolder.setText(R.id.tv_title, BatchNo + " | " + AssetNo)
            viewHolder.setText(R.id.tv_text1, context.getString(R.string.status) + "：" + StatusName)
            viewHolder.setText(R.id.tv_text2, context.getString(R.string.label_tag) + "：" + LabelTag)
            viewHolder.setText(R.id.tv_text3, context.getString(R.string.progress) + "：")
            viewHolder.setText(R.id.tv_text4, "${ScanNum}" + "/" + "${LabelTagNum}")
            viewHolder.setGone(R.id.tv_text5, true)

            val myListView = viewHolder.getView<MYListView>(R.id.listview)
            if (!content.isNullOrEmpty()) {
                myListView.visibility = View.VISIBLE
                val listBean = ArrayList<DataBean>()
                val jsonArray = JSONArray(content)
                val limit = minOf(jsonArray.length(), 6)
                for (i in 0 until limit) {
                    val opt = jsonArray.optJSONObject(i)
                    val bean = DataBean().apply {
                        Title = opt.optString("Title")
                        content = opt.getString("content")
                    }
                    listBean.add(bean)
                }
                myListView.run {
                    setClickable(false)
                    setPressed(false)
                    setEnabled(false)
                    adapter = StockTextAdapter(
                        context,
                        listBean
                    )
                }
            } else {
                myListView.visibility = View.GONE
            }

            viewHolder.setVisible(R.id.tv_detailed, true)
            viewHolder.getView<RoundTextView>(R.id.tv_detailed).setOnClickListener {
                mItemClickListener?.onDetailedClick(content, AssetNo)
            }
        }
    }

    var mFilterList = ArrayList<TaskStockListBean>()

    fun appendList(list: List<TaskStockListBean>) {
        data = list as MutableList<TaskStockListBean>
        //这里需要初始化filterList
        mFilterList = list as ArrayList<TaskStockListBean>
    }

    override fun getFilter(): Filter {
        return object : Filter() {
            //执行过滤操作
            override fun performFiltering(charSequence: CharSequence): FilterResults {
                val charString = charSequence.toString()
                if (charString.isEmpty()) {
                    //没有过滤的内容，则使用源数据
                    mFilterList = data as ArrayList<TaskStockListBean>
                } else {
                    val filteredList: MutableList<TaskStockListBean> = ArrayList()
                    for (i in data.indices) {
                        val bean = data[i]
                        if (   bean.LabelTag?.contains(charString, ignoreCase = true) == true
                            || bean.BatchNo?.contains(charString, ignoreCase = true) == true
                            || bean.AssetNo?.contains(charString, ignoreCase = true) == true
                        ) {
                            filteredList.add(bean)
                        }
                    }
                    mFilterList = filteredList as ArrayList<TaskStockListBean>
                }
                val filterResults = FilterResults()
                filterResults.values = mFilterList
                return filterResults
            }

            //把过滤后的值返回出来
            override fun publishResults(charSequence: CharSequence, filterResults: FilterResults) {
                mFilterList = filterResults.values as ArrayList<TaskStockListBean>
                notifyDataSetChanged()
            }
        }
    }


    override fun getItemCount(): Int {
        return mFilterList.size
    }

    override fun hashCode(): Int {
        return mFilterList.hashCode()
    }

    var mItemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(l: OnItemClickListener?) {
        mItemClickListener = l
    }

    interface OnItemClickListener {
        fun onDetailedClick(content: String?, AssetNo: String?)
    }

}